package com.kaoyaya.delivery.activity;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.kaoyaya.delivery.fragment.CartFmt_;
import com.kaoyaya.delivery.fragment.ComboFmt_;
import com.kaoyaya.delivery.fragment.CostRecordFmt_;
import com.kaoyaya.delivery.fragment.DeliveryRecordFmt_;
import com.kaoyaya.delivery.fragment.LeftFragment_;
import com.kaoyaya.delivery.fragment.ProductListFmt_;
import com.kaoyaya.delivery.fragment.RightFragment;
import com.kaoyaya.view.SlidingMenu;

public class SlidingActivity extends FragmentActivity {
	
	boolean isExit;// 是否已退出应用的标志
	SlidingMenu slidingMenu;
	LinearLayout llNotice;
	RelativeLayout llMain;
	ImageView ivArrow,ivHand;
	LeftFragment_ leftFmt;
	RightFragment rightFmt;
	Fragment centerFmt;
	FragmentManager  fManager;
	Map<String, Fragment> fmtMap;
	ActivityApp app;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_sliding);
		app = (ActivityApp) getApplication();
		app.addActivity(this);
		init();
		initListener();
	}

	private void init() {
		llMain = (RelativeLayout) findViewById(R.id.llMain);
		llMain.setOnClickListener(clickListener);
		llNotice = (LinearLayout) findViewById(R.id.llNotice);
		ivArrow = (ImageView) findViewById(R.id.ivArrow);
		ivHand = (ImageView) findViewById(R.id.ivHand);
		popNotice();
		llNotice.setVisibility(View.GONE);
		
		slidingMenu = (SlidingMenu) findViewById(R.id.slidingMenu);
		slidingMenu.setLeftView(getLayoutInflater().inflate(R.layout.frame_left, null));
		slidingMenu.setRightView(getLayoutInflater().inflate(R.layout.frame_right, null));
		slidingMenu.setCenterView(getLayoutInflater().inflate(R.layout.frame_center, null));

		fmtMap =new HashMap<String, Fragment>();
		fmtMap.put("productList", new ProductListFmt_());
		fmtMap.put("combo", new ComboFmt_());
		fmtMap.put("cart", new CartFmt_());
		fmtMap.put("deliveryRecord", new DeliveryRecordFmt_());
		fmtMap.put("costRecord", new CostRecordFmt_());
		
		fManager = this.getSupportFragmentManager();
		FragmentTransaction fTransaction = fManager.beginTransaction();
		leftFmt = new LeftFragment_();
		fTransaction.replace(R.id.fl_left, leftFmt);

		rightFmt = new RightFragment();
		fTransaction.replace(R.id.fl_right, rightFmt);

		centerFmt = fmtMap.get("productList");
		fTransaction.replace(R.id.fl_center, centerFmt);
		fTransaction.commit();
	}
	
	public void updateBalance() {
		leftFmt.updateBalance();
	}
	
	public void updateCart() {
		leftFmt.updateCart();
	}
	
	public void updateContent(String tag, boolean fromLeft) {
		if ("recharge".equals(tag)) {
			Uri u = Uri.parse("https://me.alipay.com/kaoyaya");
			Intent intent = new Intent(Intent.ACTION_VIEW, u);
			startActivity(intent);
		} else {
			centerFmt = fmtMap.get(tag);
			Fragment oldFmt = fManager.findFragmentById(R.id.fl_center);
			if (centerFmt != null && centerFmt != oldFmt) {
				FragmentTransaction fTransaction = fManager.beginTransaction();
				if (oldFmt == fmtMap.get("productList")) {
					fTransaction.addToBackStack(null);
				}
				fTransaction.replace(R.id.fl_center, centerFmt);
				fTransaction.commit();
			}
			if (fromLeft) {
				showLeft();
			}
		}
		// centerTag = tag;
		//initListener();
	}
	
	View.OnClickListener clickListener = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			llNotice.setVisibility(View.GONE);
		}
	};

	private void initListener() {
		/*centerFragment.setPageChangeCallback(new CenterFragment.PageChangeCallback() {
			@Override
			public void onPageChanged(int position) {
				// 当滑动第一个选项卡时,激活左边的碎片
				if (centerFragment.isFirst()) {
					slidingMenu.setCanSliding(true, false);
			    // 当滑动最后一个选项卡时,激活右边的碎片
				} else if (centerFragment.isEnd()) {
					// 第二个参数设为false,使右边的碎片永远不能通过滑动来激活
					slidingMenu.setCanSliding(false, false);
				} else {
					slidingMenu.setCanSliding(false, false);
				}
			}
		});*/
	}

	public void showLeft() {
		slidingMenu.showLeftView();
	}

	public void showRight() {
		slidingMenu.showRightView();
	}

	void popNotice() {
/*		int[] location = new  int[2] ;
		ivArrow.getLocationInWindow(location);
		int x = location[0];
		int y = location[1];
		int width = ivArrow.getWidth();
		int height = ivArrow.getHeight();
		TranslateAnimation animation = new TranslateAnimation(x, x + width, y + height / 2, y + height / 2);
		animation.setDuration(2000);
		ivHand.setVisibility(View.VISIBLE);
		ivHand.startAnimation(animation);*/
	}
	
/*	@Override    
    public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			if (llNotice.getVisibility() == View.VISIBLE) {
				llNotice.setVisibility(View.GONE);
			}
		}
		return true;    
	}*/
	
	@Override
	public void onStart() {
		super.onStart();
//		popNotice();
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (slidingMenu.isLeftShow()) {
				showLeft();
			} else if (!fManager.popBackStackImmediate()) {
				exitBy2Click(); // 调用双击退出函数
			}
		}
		return false;
	}
	
	/**
	 * 双击退出
	 */
	private void exitBy2Click() {
		if (!isExit) {
			isExit = true; // 准备退出
			Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
			new Timer().schedule(new TimerTask() {
				@Override
				public void run() {
					isExit = false; // 取消退出
				}
			}, 2000); // 如果2秒钟内没有按下返回键，则启动定时器取消掉刚才执行的任务
		} else {
			app.finishAll();
		}
	}
	
}
